package com.airbnb.mvrx.sample.features.book

import android.graphics.Color
import android.os.Bundle
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.airbnb.mvrx.fragmentViewModel
import com.airbnb.mvrx.sample.core.BaseFragment
import com.airbnb.mvrx.sample.core.simpleController
import com.airbnb.mvrx.sample.features.book.models.*
import com.airbnb.mvrx.sample.features.book.views.*
import com.airbnb.mvrx.sample.views.loadingRow
import com.alibaba.fastjson.JSON
import com.example.mvrxstudy.R
import com.yt.ytdeep.client.dto.BookDTO
import com.yt.ytdeep.client.dto.BookHomeDetailDTO
import com.yt.ytdeep.client.dto.LessonDTO


class BookDetailFragment : BaseFragment() {

    private val bookDetailViewModel: BookDetailViewModel by fragmentViewModel()
    private val recommendLessonViewModel: RecommendLessonViewModel by fragmentViewModel()
    private val recommendBookVM: RecommendBookViewModel by fragmentViewModel()
    private var paletteColor: Int = 0
    private var bookName: String = ""

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val rootView = super.onCreateView(inflater, container, savedInstanceState)
        toolbar.setTitleTextColor(Color.WHITE)
        var scrollY = 0
        recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {

            override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                scrollY += dy
                adjustTitleBgAlpha(scrollY)
            }
        })
        return rootView
    }

    override fun getResourceId(): Int {
        return R.layout.fragment_book_detail
    }

    override fun epoxyController() = simpleController(
            bookDetailViewModel,
            recommendLessonViewModel,
            recommendBookVM) { bookDetailState,
                               recommendLessonState,
                               recommendBookState ->
        val resp = bookDetailState.resp()
        if (resp == null) {
            loadingRow {
                id("loading")
            }
            return@simpleController
        }
        if (bookDetailState.resp.complete
        ) {
            var dataStr = JSON.toJSONString(resp?.data)
            val bookDetailDTO: BookHomeDetailDTO = JSON.parseObject(dataStr, BookHomeDetailDTO::class.java)
            bookName = bookDetailDTO.bookDTO.name
            bookDetailHeader {
                id("header")
                thumb(bookDetailDTO.bookDTO.thumbnails)
                bookName(bookDetailDTO.bookDTO.name)
                author(bookDetailDTO.bookDTO.author)
                publisher(bookDetailDTO.bookDTO.publisher)
                paletteCallback(object : BookDetailHeader.PaletteCallback {
                    override fun getRgb(rgb: Int) {
                        paletteColor = rgb
                    }

                })
            }
            bookDetailFuncList {
                id("funcList")
                list(bookDetailViewModel.getFuncList(bookDetailDTO))
            }
            bookDetailIntro {
                id("intro")
                desc(bookDetailDTO.bookDTO.description)
            }
            if (recommendLessonState.resp.complete) {
                dataStr = JSON.toJSONString(recommendLessonState.resp()?.data)
                val lessonList: List<LessonDTO> = JSON.parseArray(dataStr, LessonDTO::class.java)
                val lessonDTO = lessonList[0]
                bDRecommendLesson {
                    id("lesson")
                    image(lessonDTO.thumbnails)
                    title(lessonDTO.name)
                    content(lessonDTO.description)
                    price(PriceModel(lessonDTO.authType, lessonDTO.authVal))
                    learned(lessonDTO.viewNum)
                }
            }
            if (recommendBookState.resp.complete) {
                dataStr = JSON.toJSONString(recommendBookState.resp()?.data)
                val bookList: List<BookDTO> = JSON.parseArray(dataStr, BookDTO::class.java)
                bDRecommendBook {
                    id("book")
                    list(bookList)
                }
            }

        }
    }


    fun adjustTitleBgAlpha(scrollY: Int) {
        if (paletteColor != 0) {
            toolbar.setBackgroundColor(bookDetailViewModel.getNewArgbColor(scrollY, paletteColor))
            toolbar.title = if (scrollY >= bookDetailViewModel.getTitleShowPosition()) bookName else ""
        }

    }
}